// 2013-9-1下午01:08:28  net.betou.trade.main.manager.impl.ChannelMngImpl.java

package net.betou.trade.main.manager.impl;

import java.util.ArrayList;
import java.util.List;

import net.betou.common.hibernate3.Updater;
import net.betou.common.page.Pagination;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Channel;
import net.betou.trade.main.dao.ChannelDao;
import net.betou.trade.main.manager.ChannelMng;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class ChannelMngImpl implements ChannelMng{

	@Transactional( readOnly = true )
	public List< Channel > getList( AgentAndAdmins admin ) {
		boolean system = admin.getSystem( );

		if( system ){
			return dao.getList( null );
		}

		return dao.getList( admin.getId( ) );
	}

	@Transactional( readOnly = true )
	public Pagination getPage( AgentAndAdmins admin, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize ) {

		boolean system = admin.getSystem( );

		if( system ){
			return dao.getPage( null, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
		}

		return dao.getPage( admin.getId( ), searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );

	}

	public Channel save( Channel bean, AgentAndAdmins admin, Integer parentId, Integer [ ] userGroup, Integer [ ] smsGroups ) {

		Channel parent = null;

		if( parentId != null && parentId > 0 ){
			parent = dao.findById( parentId );
		}
		
		bean.setOrientedGroup( splitGroup( userGroup ) );
		
		// 设置短信发送group
		if( smsGroups != null ){
			bean.setSmsGroup( splitGroup( smsGroups ) );
		}
		
		bean.init( parent );

		bean.setAdmin( admin );

		return dao.save( bean );
	}

	public Channel deleteById( Integer id ) {
		return dao.deleteById( id );
	}

	public Channel [ ] deleteByIds( Integer [ ] ids ) {
		Channel [ ] beans = new Channel [ ids.length ];

		for ( int i = 0; i < ids.length; i++ ){
			beans[ i ] = dao.deleteById( ids[ i ] );
		}
		return beans;
	}

	public Channel update( Channel bean, Integer [ ] userGroup, Integer parentId, Integer [ ] smsGroups ) {

		String group = splitGroup( userGroup );
		
		bean.setOrientedGroup( group );
		
		if( parentId != null && parentId > 0 ){
			bean.setParent( dao.findById( parentId ) );
		}
		
		if( smsGroups != null ){
			bean.setSmsGroup( splitGroup( smsGroups ) );
		}
		
		Updater< Channel > updater = new Updater< Channel >( bean, Updater.UpdateMode.MIN );
		
		
		updater.include( "name" ); // 使用最小模式，只更新名称，其余全都不更新
		updater.include( "priority" );
		updater.include( "orientedGroup" );
		updater.include( "parent" );
		updater.include( "smsGroup" );

		return dao.updateByUpdater( updater );
	}

	@Transactional( readOnly = true )
	public Channel findById( Integer id ) {
		return dao.findById( id );
	}

	@Transactional( readOnly = true )
	public Channel findMyChannel( Integer id, Integer adminId, boolean system ) {

		if( system ){
			return dao.findById( id );
		}
		return dao.findMyChannel( id, adminId );

	}
	
	private String splitGroup( Integer [ ] userGroup ){
		String group = "";
		if( userGroup != null ){
			for ( int i = 0; i < userGroup.length; i++ ){
				group += userGroup[ i ];
				if( i < userGroup.length - 1 ){
					group += ",";
				}
			}
		}
		
		return group;
	}
	
	@Transactional( readOnly = true )
	public List< Integer > getOrientedGroup( Integer groupId, List< Integer > idList ){
		List< Channel > list = dao.getOrientedGroupList( idList );
		List< Integer > channelIdList = new ArrayList< Integer >( );
		
		// 获取当前用户分组的面向分组的栏目ID
		if( list != null && !list.isEmpty( ) ){
			for( Channel c : list ){
				String group = c.getOrientedGroup( );
				String [ ] groupArr = StringUtils.split( group, "," );
				
				for( String g : groupArr ){
					if( g.equals( groupId.toString( ) )){
						channelIdList.add( c.getId( ) ); 
						break;
					}
				}
			}
		}
		
		return channelIdList;
		
	}

	@Autowired
	private ChannelDao dao;
}
